﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_Model;
using V5_BLL;

namespace V5_EShop.Admin.ProductExpand {
    public partial class Admin_ProductFieldClass : BasicPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Action = InputHelper.SetString(Request["Action"]);
                switch (Action.Trim().ToLower()) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_gvList();
                        break;
                }
            }
        }

        private void Bind_gvList() {
            this.MultiViewList.ActiveViewIndex = 0;
            this.gvList.DataSource = new BLLProductFieldClass().GetAllList();
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelProductFieldClass model = new BLLProductFieldClass().GetModel(id);
            if (model != null) {
                this.hfID.Value = model.ID.ToString();
                this.txtProductFieldName.Text = model.FieldClassName;
                this.lblTitle.Text = "修改分类";
                this.btnSave.Text = "修改保存";
            }
        }

        private void Bind_Delete(int id) {
            new BLLProductFieldClass().Delete(id);
            this.Bind_gvList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {

            ModelProductFieldClass model = new ModelProductFieldClass();
            BLLProductFieldClass bll = new BLLProductFieldClass();
            int id = InputHelper.SetNumber(this.hfID.Value);
            string ClassName = this.txtProductFieldName.Text;
            int isSelected = InputHelper.SetNumber(this.ddlIsSelected.SelectedValue);
            model.FieldClassName = ClassName;
            model.IsSelected = isSelected;
            if (id == 0) {
                bll.Add(model);
            }
            else if (id > 0) {
                model.ID = id;
                bll.Update(model);
            }
            Bind_gvList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            int sid = InputHelper.SetNumber(e.CommandArgument);
            switch (eName.Trim().ToLower()) {
                case "go":
                    Bind_DataEdit(sid);
                    break;
                case "del":
                    Bind_Delete(sid);
                    break;
                case "gosub":
                    Response.Redirect("Admin_ProductField.aspx?fclass=" + sid);
                    break;
            }
        }

    }
}